package com.xunkids.bos.migration.component.handler;


import com.xunkids.bos.migration.common.exception.RushException;
import com.xunkids.bos.migration.component.log.LoggerHelp;
import com.xunkids.bos.migration.component.vo.Result;
import org.slf4j.Logger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

/**
 * web 统一异常处理
 *
 * @author lihe
 */

@Order(Integer.MAX_VALUE - 1)
@ControllerAdvice(basePackages = {"com.xunkids.bos.migration"})
@ConditionalOnClass(RequestMappingHandlerAdapter.class)
public class GlobalExceptionHandler {

    private static final Logger log = LoggerHelp.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public <T> Result<T> handleException(Throwable e) {
        log.error("异常信息为: " ,e);
        return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(RushException.class)
    public <T> Result<T> handleRushException(Throwable e) {
        log.error("异常信息为: " ,e);
        return Result.fail(HttpStatus.TOO_MANY_REQUESTS, e.getMessage());
    }



}